﻿
using Dapper;
using hentity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
 namespace hdal {
       public abstract class AbsWCSServer :AbsWMSBaseServer, IWCSServer {
       
          public List<BStn> GetPhysicalStns() {
            string sql = "select * from  b_stn";
            return Connection.Query<BStn>(sql).ToList();
        }
        public List<BStnRelation> GetPhysicalStnRelation() {
            string sql = "select * from  b_stn_relation where disable=0";
            return Connection.Query<BStnRelation>(sql).ToList();
        }
         public void updatePhysicalStn(BStn stn) {
            String sql = "update b_stn set eid=eid ";
            if( stn.length > 0 ) {
                sql += " ,length=@length ";
            }
            if( stn.x > 0 ) {
                sql += " ,x=@x ";
            }
            if( stn.y > 0 ) {
                sql += " ,y=@y ";
            }
            sql += " where eid = @eid ";
            Connection.Execute(sql ,stn);
          }

   

        public void updatePos(int x,int y ,String ids) {
            String sql = "update b_stn set ";
            if( x > 0 ) {

                sql += "x=@x,";
            } else {
                sql += "x= x,";
            }
            if( y > 0 ) {
                sql += "y=@y";
            } else {
                sql += "y= y";
            }
            sql += " where eid in  @idArr";
            String[] idArr = ids.Split(',');
            Connection.Execute(sql ,new { x,y ,idArr });
        }
        public BParam getParam(string driveType ,int driveId ,string key) {
            string sql = "select * from b_Param where drive_Type=@driveType and drive_index=@driveId and key_Code=@key";
            BParam mechineStatus = Connection.QueryUnique<BParam>(sql ,new { driveType ,driveId ,key });
            return mechineStatus;
        }
        public List<BParam> getParams(string driveType ,int driveId) {
            string sql = "select * from b_Param  where 1=1 ";
            if( !String.IsNullOrWhiteSpace(driveType) ) {

                sql += " and drive_Type=@driveType";
            }

            if( driveId>0 ) {

                sql += " and drive_index=@driveId";
            }
            return Connection.Query<BParam>(sql ,new { driveType,driveId }).ToList();
        }
        public List<BParam> getParams() {
            string sql = "select * from b_Param";
            return Connection.Query<BParam>(sql ,new { }).ToList();
        }
         public bool update(string driveType ,int driveId ,string key ,int value) {
            string sql = "update b_Param set v=@value where  drive_Type=@driveType  and key_Code=@key ";
            if( driveId != 0 ) {
                sql += " and drive_index = @driveId";
            }
            int i = Connection.Execute(sql ,new { value ,driveType ,key ,driveId });
            if( i != 1 ) {
                return false;
            } else {
                return true;
            }
        }
       
    }
}
